package Stack;

import java.math.BigDecimal;

/**
 * @author yyq
 * @create 2021-10-08 16:14
 */
public class StackComputer<T> {
    private int Max_Size;
    //top始终指向数据上方
    private int top;
    private Object[] stack;
    private static ArrayStack<Character> s2=new ArrayStack<Character>(100);

    public StackComputer(int max_Size) {
        top=0;
        Max_Size = max_Size;
        stack=new Object[max_Size];

    }

    public void push(T val){
        //检查是否超过Maxsize
        if(top<Max_Size){
            stack[top]=val;
            top++;
        }
        else{
            System.out.println("超过最大容量，注入失败");
        }
    }

    public Boolean isEmpty(){
        if(top==0) return true;
        else return false;
    }

    public  T pop() {
        if(top==0){
            System.out.println("没得数据了，老表");
            return null;
        }
        if(top!=0){
            top--;
            return (T) stack[top];
        }
        return null;
    }

    public int getTop(){
        return top;
    }
// public BigDecimal cumpteStack(){
//
//        BigDecimal num1=new BigDecimal((char[]) pop());
//        BigDecimal num2=new BigDecimal((char[]) pop());
//        Character fh = (Character) pop();
//        while (true){
//          num1=compute(num1,num2,fh);
//            if (isEmpty()){
//                return num1;
//            }
//          num2=(BigDecimal) pop();
//          fh=(Character)pop();
//        }
//
//    }

    public BigDecimal compute(BigDecimal  a,BigDecimal  b,Character ch){
        if(ch=='+') return a.add(b);
        if(ch=='-') return a.subtract(b);
        if(ch=='*') return a.multiply(b);
        if(ch=='/') return a.divide(b);
        return null;

    }

    public void init(String s){
        int end=0;
        for (int i = 0; i < s.length(); i++) {
            if(i==s.length()-1) end=1;
            initHz(s.charAt(i),end);
        }


    }

    public void initHz(Character ch,int end){
        //主栈为空
        if(isEmpty()){
            if(Character.isDigit(ch)){
                push((T) ch);
                return;
            }
            if (ch=='('){
                s2.push(ch);
                return;
            }
        }
        //主栈非空
        if(!isEmpty()){
            //1 数字直接进入主栈
            if(Character.isDigit(ch)){
                push((T) ch);
                if(end==1){
                   push((T) s2.pop());
                }
                return;
            }
            //2.1 ( 直接进入S2栈
            if(ch=='('){
                s2.push(ch);
                return;
            }
            //2.2 ) 不进栈 s2中的运算符出栈 直到匹配到 (
            if(ch==')'){
                Character temp=s2.pop();
                while(true){
                    if(temp=='('){
                        return;
                    }
                    push((T) temp);
                    temp=s2.pop();
                }
            }

            //3 如果是运算符
            if(ch=='+'||ch=='-'||ch=='*'||ch=='/'){
                //3.1空栈直接放入
                if(s2.isEmpty()){
                    s2.push(ch);
                    return;
                }
                //3.2 非空 运算符进栈条件 他的优先级大于栈顶的优先级
                if(!s2.isEmpty()){
                    Character temp=s2.pop();
                    int  tempPriority = getChPriority(temp);
                    int chPriority = getChPriority(ch);
                    s2.push(temp);
                    //3.2.1 入栈优先级大于s2栈顶优先级 直接入栈
                    if(chPriority>tempPriority){
                        s2.push(ch);
                    }else {
                        //3.2.2 小于s2栈顶优先级时 把s2栈顶元素push到主栈
                        while(true){
                            if(chPriority>tempPriority){
                                s2.push(ch);
                                return;
                            }else {
                                temp=s2.pop();
                                push((T) temp);
                                //考虑 s2 : * 如果此时s2空了直接入栈
                                if(s2.isEmpty()){
                                    s2.push(ch);
                                    return;
                                }
                                //考虑 s2 : - *  如果此时s2非空继续遍历
                                if(!s2.isEmpty()){
                                    temp=s2.pop();
                                    tempPriority=getChPriority(temp);
                                    s2.push(temp);
                                }

                            }
                        }

                    }

                }
            }



        }
    }

    public int getChPriority(Character value){
        int pri;
        if(value=='+') return pri=1;
        if(value=='-') return pri=1;
        if(value=='*') return pri=2;
        if(value=='/') return pri=2;
        if(value=='(') return pri=0;
        return 0;
    }

}
